package com.demo.servlet;

import com.demo.moudel.User;
import com.demo.service.UserService;
import com.demo.service.UserServiceImple;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * com.demo.servlet
 * 26832
 * demo-servlet
 * &#064;description: 处理登录的业务
 * &#064;author: lxm
 * LoginServlet
 * &#064;create: 2024-04-15 14:38
 **/

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        User user = new User();
        HttpSession session = req.getSession();
        UserService userService = new UserServiceImple();//使用接口创建对象，实例化的时候使用它的实现类
        user.setName(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        try {
            if(userService.UserLogin(user)){
                session.setAttribute("msg","success");
                req.getRequestDispatcher("./comm/Login.jsp").forward(req,resp);
            }else{
                session.setAttribute("msg","fail");
                req.getRequestDispatcher("./comm/Login.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
